
package com.enhe.gck.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.enhe.gck.com.oracle.LwdHttpConfig;
import com.enhe.gck.communal.PublicConstant;
import com.enhe.gck.communal.PublicEnum;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.fg.ConfigHWANE16627BNB;
import com.enhe.gck.fg.CustomerHWANE16644BNB;
import com.enhe.gck.item.*;
import com.enhe.gck.pojo.*;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.panel.CarbonPanel;
import com.kuangkie.carbon.record.FGRecord;
import com.kuangkie.carbon.record.FGRecordComplexusBuilder;
import com.kuangkie.carbon.record.RecordQueryFactory;
import com.kuangkie.carbon.record.RecordQueryProConJunctionFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

import static com.enhe.gck.CarbonBaseConstant.*;
import static com.enhe.gck.CarbonEnumKeyValue.*;
import static com.enhe.gck.CarbonRelationType.*;

/**
 * 进厂列表
 *
 * @author junjun
 * @date 2020-03-06 11:15:35
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VehicleOrderService {

    private final CustomerHWANE16644BNB customerHWANE16644BNB;
    private final ConfigHWANE16627BNB configHWANE16627BNB;

    /**
     * 根据订单id获取订单
     *
     * @param orderId 订单id
     * @return 订单信息
     */
    public Optional<OrderCommon> getNoCompletedOrderById(Long orderId) {
        if (null == orderId) {
            return Optional.empty();
        }
        return getNoCompletedOrderOption(orderId, null, null, null, null);
    }

    /**
     * 根据提货码获取未结束订单
     *
     * @param pickupCode 提货码
     * @return 订单
     */
    public Optional<OrderCommon> getNoCompletedOrderByPickupCode(String pickupCode) {
        if (StrUtil.isBlank(pickupCode)) {
            return Optional.empty();
        }
        return getNoCompletedOrderOption(null, null, pickupCode, null, null);
    }

    /**
     * 根据订单编号获取未结束订单
     *
     * @param orderNo 订单编号
     * @return 订单
     */
    public Optional<OrderCommon> getNoCompletedOrderByOrderNo(String orderNo) {
        if (StrUtil.isBlank(orderNo)) {
            return Optional.empty();
        }
        return getNoCompletedOrderOption(null, orderNo, null, null, null);
    }

    /**
     * 根据ic卡号获取未结束订单
     *
     * @param icCardNo ic卡号
     * @return 订单
     */
    public Optional<OrderCommon> getNoCompletedOrderByIcId(String icCardNo) {
        if (StrUtil.isBlank(icCardNo)) {
            return Optional.empty();
        }
        return getNoCompletedOrderOption(null, null, null, icCardNo,null);
    }

    /**
     * 根据车牌号获取未结束订单
     *
     * @param carNumber 车牌号
     * @return 订单
     */
    public Optional<OrderCommon> getNoCompletedOrderBycarNumber(String carNumber) {
        if (StrUtil.isBlank(carNumber)) {
            return Optional.empty();
        }
        return getNoCompletedOrderOption(null, null, null, null, carNumber);
    }

    /**
     * 根据获取未结束订单(销售订单 or 供货订单 or 倒料订单)
     *
     * @param orderNo    订单编号
     * @param pickupCode 提货点编码
     * @param icCardNo   ic卡号
     * @return 订单列表
     */
    private Optional<OrderCommon> getNoCompletedOrderOption(Long orderId, String orderNo, String pickupCode, String icCardNo, String carNumber) {
        FGRecordComplexusBuilder builder = CarbonPanel.getFGRecordComplexusBuilder();
        if(StrUtil.isNotBlank(carNumber)){
            FGRecord pouringOrderRecord = getPouringOrderRecord(orderId, orderNo, pickupCode, icCardNo, carNumber);
            if (Objects.isNull(pouringOrderRecord)) {
                return Optional.empty();
            }

            return Optional.of(OrderCommon.convertPouringOrder(builder.putRecord(pouringOrderRecord)
                    .getFGRecordComplexus()
                    .getRecordBean(pouringOrderRecord.getRecordCode(), PouringOrder.class, 3)));
        }

        // 先判断销售订单是否存在
        FGRecord orderRecord = getOrderRecord(orderId, orderNo, pickupCode, icCardNo);
        if (Objects.isNull(orderRecord)) {
            // 销售订单不存在时再判断供货订单是否存在
            FGRecord supplyOrderRecord = getSupplyOrderRecord(orderId, orderNo, pickupCode, icCardNo);
            if (Objects.isNull(supplyOrderRecord)) {
                // 供货订单也不存在时再判断倒料订单是否存在
                FGRecord pouringOrderRecord = getPouringOrderRecord(orderId, orderNo, pickupCode, icCardNo, carNumber);
                if (Objects.isNull(pouringOrderRecord)) {
                    return Optional.empty();
                }

                return Optional.of(OrderCommon.convertPouringOrder(builder.putRecord(pouringOrderRecord)
                        .getFGRecordComplexus()
                        .getRecordBean(pouringOrderRecord.getRecordCode(), PouringOrder.class, 3)));
            }

            return Optional.of(OrderCommon.convertSupplyOrder(builder.putRecord(supplyOrderRecord)
                    .getFGRecordComplexus()
                    .getRecordBean(supplyOrderRecord.getRecordCode(), SupplyOrder.class, 3)));
        }

        OrderCommon order = OrderCommon.convertOrder(builder.putRecord(orderRecord)
                .getFGRecordComplexus()
                .getRecordBean(orderRecord.getRecordCode(), Order.class, 3));
        // 设置客商剩余
        fillInCustomerInformation(order);
        return Optional.of(order);
    }

    /**
     * 填充订单的客户信息(客商剩余吨数/剩余袋数)
     *
     * @param order 订单
     */
    public void fillInCustomerInformation(OrderCommon order) {
        if (Objects.nonNull(order.getCustomerId()) && Objects.nonNull(order.getCompanyName()) && Objects.nonNull(order.getCustomerType())) {
            Long customerType = Long.valueOf(order.getCustomerType());
            Long relation = null;
            if (Objects.equals(customerType, 客户管理_客户类型_经销商_jxs)) {
                relation = RR_订单记录_客户名称_客户管理;
            } else if (Objects.equals(customerType, 客户管理_客户类型_散户_sh)) {
                relation = RR_订单记录_散户_客户管理;
            }
            if (Objects.nonNull(relation)) {
                FGRecord customerFgRecord = CarbonPanel.getRecordDiscover(客户管理_khgl).getRecord(String.valueOf(order.getCustomerId()));
                List<BigDecimal> weightList = customerHWANE16644BNB.getWeight(customerFgRecord, relation);
                // 设置客商剩余-重量
                order.setWeightUsable(weightList.get(0).doubleValue());
                String value = configHWANE16627BNB.getConfigValueOneWeight();
                BigDecimal oneWeight = new BigDecimal(value);
                if (oneWeight.compareTo(BigDecimal.ZERO) > 0) {
                    // 设置客商剩余-袋数
                    order.setBagUsable(String.valueOf(Integer.valueOf(PublicMethod.divideBigDecimal(weightList.get(0), value, 0, RoundingMode.DOWN).toPlainString())));
                }
            }
        }
    }

    /**
     * 获取倒料订单(可能完成,可能未完成)
     *
     * @param orderNo 订单号
     * @return 倒料订单
     */
    public Optional<OrderCommon> getPouringOrderByOrderNo(String orderNo) {
        FGRecord fgRecord = CarbonPanel.getRecordQueryFactory(倒料订单_dldd).getProConJunctionFactory()
                .addEqual(PouringOrderHWANE18490Item.基本属性组_单号, orderNo)
                .goBack().queryARecord();
        if (Objects.isNull(fgRecord)) {
            return Optional.empty();
        }
        return Optional.of(OrderCommon.convertPouringOrder(CarbonPanel.getFGRecordComplexusBuilder().putRecord(fgRecord)
                .getFGRecordComplexus()
                .getRecordBean(fgRecord.getRecordCode(), PouringOrder.class, 2)));
    }

    private FGRecord getPouringOrderRecord(Long orderId, String orderNo, String pickupCode, String icCardNo,String carNumber) {
        RecordQueryProConJunctionFactory supplyOrderProFactory = CarbonPanel.getRecordQueryFactory(倒料订单_dldd).getProConJunctionFactory();
        if (null != orderId) {
            supplyOrderProFactory.addEqual(COLUMN_ABP0001, orderId);
        }
        if (StrUtil.isNotBlank(orderNo)) {
            supplyOrderProFactory.addEqual(PouringOrderHWANE18490Item.基本属性组_单号, orderNo);
        }
        if (StrUtil.isNotBlank(pickupCode)) {
            log.warn("倒料订单不支持根据提货点编码查询;提货码:[{}]", pickupCode);
            return null;
        }
        if (StrUtil.isNotBlank(icCardNo)) {
            supplyOrderProFactory.addEqual(PouringOrderHWANE18490Item.基本属性组_IC卡号, icCardNo);
        }
        if (StrUtil.isNotBlank(carNumber)) {
            supplyOrderProFactory.getRelationCriterionFactory(车辆管理_clgl)
                    .setInRelationTypes(RR_倒料订单_车牌号_车辆管理)
                    .getGuestJunctionFactory()
                    .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNumber);
        }

        return supplyOrderProFactory
                .addNotEqual(PouringOrderHWANE18490Item.基本属性组_状态, 订单记录_状态_已出厂_ycc)
                .addNotEqual(PouringOrderHWANE18490Item.基本属性组_是否作废, true)
                .goBack().queryARecord();
    }

    private static FGRecord getSupplyOrderRecord(Long orderId, String orderNo, String pickupCode, String icCardNo) {
        RecordQueryProConJunctionFactory supplyOrderProFactory = CarbonPanel.getRecordQueryFactory(供货订单_ghdd).getProConJunctionFactory();
        if (null != orderId) {
            supplyOrderProFactory.addEqual(COLUMN_ABP0001, orderId);
        }
        if (StrUtil.isNotBlank(orderNo)) {
            supplyOrderProFactory.addEqual(SupplyOrderHWANE16829Item.基本属性组_单号, orderNo);
        }
        if (StrUtil.isNotBlank(pickupCode)) {
            supplyOrderProFactory.addEqual(SupplyOrderHWANE16829Item.基本属性组_8位取货码, pickupCode);
        }
        if (StrUtil.isNotBlank(icCardNo)) {
            supplyOrderProFactory.addEqual(SupplyOrderHWANE16829Item.基本属性组_IC卡号, icCardNo);
        }

        return supplyOrderProFactory
                .addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废, 是否_否_f)
                .addNotEqual(SupplyOrderHWANE16829Item.基本属性组_状态, 订单记录_状态_已出厂_ycc)
                .goBack().queryARecord();
    }

    private static FGRecord getOrderRecord(Long orderId, String orderNo, String pickupCode, String icCardNo) {
        RecordQueryProConJunctionFactory orderProFactory = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();
        if (null != orderId) {
            orderProFactory.addEqual(COLUMN_ABP0001, orderId);
        }
        if (StrUtil.isNotBlank(orderNo)) {
            orderProFactory.addEqual(OrderHWANE16677Item.基本属性组_单号, orderNo);
        }
        if (StrUtil.isNotBlank(pickupCode)) {
            orderProFactory.addEqual(OrderHWANE16677Item.基本属性组_8位取货码, pickupCode);
        }
        if (StrUtil.isNotBlank(icCardNo)) {
            orderProFactory.addEqual(OrderHWANE16677Item.基本属性组_IC卡号, icCardNo);
        }

        return orderProFactory
                .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                .addNotEqual(OrderHWANE16677Item.基本属性组_状态, 订单记录_状态_已出厂_ycc)
                .goBack().queryARecord();
    }

    /**
     * 根据订单编号获取订单集合
     *
     * @param orderNo 订单编号
     * @return 订单信息
     */
    public Optional<List<OrderCommon>> getOrderInfosByOrderNos(List<String> orderNo) {
        CarbonPanel.changeCurrentProgram(Long.valueOf(LwdHttpConfig.programCode));
        if (CollectionUtils.isEmpty(orderNo)) {
            return Optional.empty();
        }
        //根据订单号查询订单信息
        RecordQueryProConJunctionFactory orderProFactory = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();


        //查询订单信息
        orderProFactory.addInclude(OrderHWANE16677Item.基本属性组_单号, orderNo);
        List<FGRecord> orderRecords = orderProFactory.goBack().queryRecords();
        if (CollectionUtils.isEmpty(orderNo)) {
            return Optional.empty();
        }

        List<Order> orders = orderRecords.stream().map(record -> record.getBean(Order.class)).collect(Collectors.toList());

        List<OrderCommon> orderCommons = orderRecords.stream().map(order -> {

            Order recordBean = CarbonPanel.getFGRecordComplexusBuilder().putRecord(orderRecords)
                    .getFGRecordComplexus()
                    .getRecordBean(order.getRecordCode(), Order.class, 2);

            OrderCommon orderCommon = OrderCommon.convertOrder(recordBean);
            System.out.println(orderCommon.getCompanyName());
            return orderCommon;
        }).collect(Collectors.toList());

        return Optional.of(orderCommons);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void updateOrderState(Long orderId, long orderState, String tableKey, String orderStateKey) {
        IntegrationMsg msg = CarbonPanel.getFGRecordBuilder(tableKey, String.valueOf(orderId)).putAttribute(orderStateKey, orderState).integrate();
        if (msg.success()) {
            log.warn("更新订单状态成功");
        } else {
            log.error("更新订单状态失败:{}", msg.getRefuseMsgContentStr());
        }
    }

    /**
     * 判断车辆是否有已过磅或待过磅的未结束的订单
     *
     * @param carNo          车牌号
     * @param excludeOrderNo 排除订单号
     * @return true:有订单;false:无订单
     */
    public boolean hasWeighedOrWaiting(String carNo, String excludeOrderNo) {
        List<OrderCommon> orderCommonList = new ArrayList<>();
        List<String> targetOrderStateList = Arrays.asList(String.valueOf(订单记录_状态_待装卸货_dzxh), String.valueOf(订单记录_状态_待过二磅_dgeb));

        // 先判断销售订单是否存在
        RecordQueryProConJunctionFactory orderProFactory = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();
        orderProFactory
                .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                .addEqual(OrderHWANE16677Item.基本属性组_是否退货, 是否_否_f)
                .addNotEqual(OrderHWANE16677Item.基本属性组_单号, excludeOrderNo)
                .addInclude(OrderHWANE16677Item.基本属性组_状态, targetOrderStateList)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_订单记录_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<Order> orderList = orderProFactory.goBack().queryBeans(Order.class);
        if (CollectionUtil.isNotEmpty(orderList)) {
            log.warn("订单记录中存在未结束的订单,相关订单号:{}", JSONUtil.toJsonStr(orderList.stream().map(Order::getOrder).collect(Collectors.toList())));
            return true;
        }

        // 再判断采购订单是否存在
        RecordQueryProConJunctionFactory supplyOrderProFactory = CarbonPanel.getRecordQueryFactory(供货订单_ghdd).getProConJunctionFactory();
        supplyOrderProFactory
                .addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废, 是否_否_f)
                .addEqual(SupplyOrderHWANE16829Item.基本属性组_是否退货, 是否_否_f)
                .addNotEqual(SupplyOrderHWANE16829Item.基本属性组_单号, excludeOrderNo)
                .addInclude(SupplyOrderHWANE16829Item.基本属性组_状态, targetOrderStateList)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_供货订单_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<SupplyOrder> supplyOrderList = orderProFactory.goBack().queryBeans(SupplyOrder.class);
        if (CollectionUtil.isNotEmpty(supplyOrderList)) {
            log.warn("供货订单记录中存在未结束的订单,相关订单号:{}", JSONUtil.toJsonStr(supplyOrderList.stream().map(SupplyOrder::getOrder).collect(Collectors.toList())));
            return true;
        }

        // 最后判断倒料订单
        RecordQueryProConJunctionFactory pouringOrderProFactory = CarbonPanel.getRecordQueryFactory(倒料订单_dldd).getProConJunctionFactory();
        pouringOrderProFactory
                .addEqual(PouringOrderHWANE18490Item.基本属性组_是否作废, 是否_否_f)
                .addNotEqual(PouringOrderHWANE18490Item.基本属性组_单号, excludeOrderNo)
                .addInclude(PouringOrderHWANE18490Item.基本属性组_状态, targetOrderStateList)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_倒料订单_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<PouringOrder> pouringOrderList = orderProFactory.goBack().queryBeans(PouringOrder.class);
        boolean notEmpty = CollectionUtil.isNotEmpty(pouringOrderList);
        if (notEmpty) {
            log.warn("倒料订单记录中存在未结束的订单,相关订单号:{}", JSONUtil.toJsonStr(pouringOrderList.stream().map(PouringOrder::getOrder).collect(Collectors.toList())));
        }
        return notEmpty;
    }

    /**
     * 根据车牌号查询待过磅未结束的订单信息
     *
     * @param carNo
     * @return
     */
    public Optional<Order> getOrderNoByCarNumber(String carNo) {
        List<OrderCommon> orderCommonList = new ArrayList<>();
        List<String> targetOrderStateList = Arrays.asList(String.valueOf(订单记录_状态_待装卸货_dzxh), String.valueOf(订单记录_状态_待过二磅_dgeb));

        // 先判断销售订单是否存在
        RecordQueryProConJunctionFactory orderProFactory = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();
        orderProFactory
                .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                .addInclude(OrderHWANE16677Item.基本属性组_状态, targetOrderStateList)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_订单记录_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<Order> orderList = orderProFactory.goBack().queryBeans(Order.class);
        if (CollectionUtils.isEmpty(orderList)) {
            return Optional.empty();
        }
        if (orderList.size() > 1) {
            return Optional.empty();
        }
        return Optional.of(orderList.get(0));
    }

    public Optional<OrderCommon> getNoCompletedSaleOrderByIcId(String icId) {
        FGRecordComplexusBuilder builder = CarbonPanel.getFGRecordComplexusBuilder();

        // 先判断销售订单是否存在
        FGRecord orderRecord = getOrderRecord(null, null, null, icId);
        if (Objects.isNull(orderRecord)) {
            return Optional.empty();
        }

        OrderCommon order = OrderCommon.convertOrder(builder.putRecord(orderRecord)
                .getFGRecordComplexus()
                .getRecordBean(orderRecord.getRecordCode(), Order.class, 1));
        // 查询车辆信息
        RecordQueryFactory carFactory = CarbonPanel.getRecordQueryFactory(车辆管理_clgl);
        carFactory.getProConJunctionFactory()
                .getRelationCriterionFactory(订单记录_ddjl)
                .setInRelationTypes(RR_车辆管理_属于订单记录_订单记录)
                .getGuestJunctionFactory()
                .addEqual(COLUMN_ABP0001, order.getId());
        Car car = carFactory.queryABean(Car.class);
        if (Objects.nonNull(car)) {
            order.setVehicle(car.getCarNo());
        }
        return Optional.of(order);
    }

    public List<OrderCommon> getCompletedOrderByCarNoOrderType(String carNo, String orderType, String startTime, String endTime) {
        String tableKey;
        String orderStateKey;
        String orderCancelKey;
        long relationKey;

        boolean isSaleOrder = Objects.equals(PublicConstant.OrderTypeSale, orderType);
        boolean isSupplyOrder = Objects.equals(PublicConstant.OrderTypeSupply, orderType);

        if (isSaleOrder) {
            tableKey = 订单记录_ddjl;
            orderStateKey = OrderHWANE16677Item.基本属性组_状态;
            orderCancelKey = OrderHWANE16677Item.基本属性组_是否作废;
            relationKey = RR_订单记录_车牌号_车辆管理;
        } else {
            if (isSupplyOrder) {
                tableKey = 供货订单_ghdd;
                orderStateKey = SupplyOrderHWANE16829Item.基本属性组_状态;
                orderCancelKey = SupplyOrderHWANE16829Item.基本属性组_是否作废;
                relationKey = RR_供货订单_车牌号_车辆管理;
            } else {
                return Collections.emptyList();
            }
        }

        RecordQueryFactory queryFactory = CarbonPanel.getRecordQueryFactory(tableKey);
        queryFactory.getProConJunctionFactory()
                .addEqual(orderStateKey, 订单记录_状态_已出厂_ycc)
                .addEqual(orderCancelKey, 是否_否_f)
                .addBetween(PublicMethod.createTimeStr(tableKey), startTime, endTime)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(relationKey)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        if (isSaleOrder) {
            List<Order> orderList = queryFactory.queryBeans(Order.class, 2);
            if (CollectionUtil.isEmpty(orderList)) {
                return Collections.emptyList();
            }
            return orderList.stream().map(OrderCommon::convertOrder).collect(Collectors.toList());
        } else {
            List<SupplyOrder> supplyOrderList = queryFactory.queryBeans(SupplyOrder.class, 2);
            if (CollectionUtil.isEmpty(supplyOrderList)) {
                return Collections.emptyList();
            }
            return supplyOrderList.stream().map(OrderCommon::convertSupplyOrder).collect(Collectors.toList());
        }
    }

    /**
     * 获取未作废的订单信息
     *
     * @param orderNo 订单号
     * @return 订单信息
     */
    public Optional<OrderCommon> getOrderByNo(String orderNo) {
        // 销售订单
        if (orderNo.startsWith(PublicEnum.OrderHeader.OR.getCode())) {
            Order order = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory()
                    .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                    .addEqual(OrderHWANE16677Item.基本属性组_单号, orderNo)
                    .goBack().queryABean(Order.class, 3);
            if (Objects.nonNull(order)) {
                return Optional.of(OrderCommon.convertOrder(order));
            }
        }

        if (orderNo.startsWith(PublicEnum.OrderHeader.SM.getCode())) {
            // 供货订单
            SupplyOrder supplyOrder = CarbonPanel.getRecordQueryFactory(供货订单_ghdd).getProConJunctionFactory()
                    .addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废, 是否_否_f)
                    .addEqual(SupplyOrderHWANE16829Item.基本属性组_单号, orderNo)
                    .goBack().queryABean(SupplyOrder.class, 3);
            if (Objects.nonNull(supplyOrder)) {
                return Optional.of(OrderCommon.convertSupplyOrder(supplyOrder));
            }
        }
        if (orderNo.startsWith(PublicEnum.OrderHeader.PM.getCode())) {
            // 倒料订单
            PouringOrder pouringOrder = CarbonPanel.getRecordQueryFactory(倒料订单_dldd).getProConJunctionFactory()
                    .addEqual(PouringOrderHWANE18490Item.基本属性组_是否作废, 是否_否_f)
                    .addEqual(PouringOrderHWANE18490Item.基本属性组_单号, orderNo)
                    .goBack().queryABean(PouringOrder.class, 3);
            if (Objects.nonNull(pouringOrder)) {
                return Optional.of(OrderCommon.convertPouringOrder(pouringOrder));
            }
        }
        return Optional.empty();
    }

    public List<OrderCommon> getSaleOrderList(List<String> orderStateList, String carNo) {
        RecordQueryProConJunctionFactory qjf = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();
        qjf
                .addInclude(OrderHWANE16677Item.基本属性组_状态, orderStateList)
                .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_订单记录_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<Order> orderList = qjf.goBack().queryBeans(Order.class, 2);
        if (CollectionUtil.isNotEmpty(orderList)) {
            return orderList.stream().map(OrderCommon::convertOrder).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    public List<OrderCommon> getSaleOrderListByTypeBatchType(List<String> orderStateList, String orderType, String goodsBatchType) {
        RecordQueryProConJunctionFactory qjf = CarbonPanel.getRecordQueryFactory(订单记录_ddjl).getProConJunctionFactory();
        qjf
                .addEqual(OrderHWANE16677Item.基本属性组_类型, orderType)
                .addEqual(OrderHWANE16677Item.基本属性组_是否作废, 是否_否_f)
                .addInclude(OrderHWANE16677Item.基本属性组_状态, orderStateList)
                .getRelationCriterionFactory(产品管理_cpgl)
                .setInRelationTypes(RR_订单记录_产品物料_产品管理)
                .getGuestJunctionFactory()
                .addEqual(GoodsHWANE16621Item.基本属性组_批次类型, goodsBatchType);
        List<Order> orderList = qjf.goBack().queryBeans(Order.class, 2);
        if (CollectionUtil.isNotEmpty(orderList)) {
            return orderList.stream().map(OrderCommon::convertOrder).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    public List<OrderCommon> getReceivingReportOrderList(List<String> stateList, String carNo) {
        RecordQueryProConJunctionFactory qjf = CarbonPanel.getRecordQueryFactory(供货订单_ghdd).getProConJunctionFactory();
        qjf
                .addInclude(SupplyOrderHWANE16829Item.基本属性组_状态, stateList)
                .addEqual(SupplyOrderHWANE16829Item.基本属性组_是否作废, 是否_否_f)
                .getRelationCriterionFactory(车辆管理_clgl)
                .setInRelationTypes(RR_供货订单_车牌号_车辆管理)
                .getGuestJunctionFactory()
                .addEqual(CarHWANE16654Item.基本属性组_车牌号, carNo);
        List<SupplyOrder> orderList = qjf.goBack().queryBeans(SupplyOrder.class, 2);
        if (CollectionUtil.isNotEmpty(orderList)) {
            return orderList.stream().map(OrderCommon::convertSupplyOrder).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}

